using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using MagicAttributeTest.Abilities;

namespace MagicAttributeTest
{
  public class CardDB
  {
    List<StaticCard> Cards;
    List<string>     ImageFiles;
    List<Ability>    Abilities;

    public List<StaticCard> GetAllCards() { return Cards; }

    public Ability GetAbility(string AbilityName)
    {
      foreach (Ability ability in Abilities)
      {
        if (ability.AbilityName == AbilityName)
          return ability;
      }
      return null;
    }

    public void InitDB()
    {
      Abilities = new List<Ability>();
      Abilities.Add(new HasteAbility());
      Abilities.Add(new TrampleAbility());
      Abilities.Add(new FirstStrikeAbility());
      Abilities.Add(new DoubleStrikeAbility());
      Abilities.Add(new FlyingAbility());
      Abilities.Add(new LifeLinkAbility());

      Cards = new List<StaticCard>();
      Cards.Add(new Bonesplitter());
      Cards.Add(new LightningBolt());
      Cards.Add(new GiantGrowth());
      Cards.Add(new RagingGoblin());
      Cards.Add(new AncientSilverback());
      Cards.Add(new LandCard("Mountain", "basic", "", Mana.ManaRed));
      Cards.Add(new LandCard("Swamp",    "basic", "", Mana.ManaBlack));
      Cards.Add(new LandCard("Plains",   "basic", "", Mana.ManaWhite));
      Cards.Add(new LandCard("Island",   "basic", "", Mana.ManaBlue));
      Cards.Add(new LandCard("Forest",   "basic", "", Mana.ManaGreen));

      foreach (StaticCard card in Cards)
      {
        card.ImagePath = card.CardName + ".full.jpg";
      }
    }

    public void ReadXmlCardFile(string FileName)
    {
      XmlDocument doc = new XmlDocument();
      doc.Load(FileName);

      XmlNodeList cardNodes = doc.SelectNodes("MagicDB/Card");
      foreach (XmlNode cardNode in cardNodes)
      {
        CreateCard(cardNode);
      }
    }
    void CreateCard(XmlNode node)
    {
      StaticCard Card = new StaticCard();
      Card.CardName = node.SelectSingleNode("CardName").InnerText;
      Card.Power = System.Convert.ToInt32(node.SelectSingleNode("Power").InnerText);
      Card.Toughness = System.Convert.ToInt32(node.SelectSingleNode("Toughness").InnerText);
      Card.CastingCosts.SetManaCosts(node.SelectSingleNode("ManaCosts").InnerText);
      foreach (XmlNode abilityNode in node.SelectNodes("OneWordAbility"))
        Card.AddAbility(GetAbility(abilityNode.InnerText));
      foreach (XmlNode typeNode in node.SelectNodes("Type"))
        Card.Types.Add(new Type(typeNode.InnerText));
      foreach (XmlNode subTypeNode in node.SelectNodes("SubType"))
        Card.Types.Add(new Type(subTypeNode.InnerText));
      foreach (XmlNode superTypeNode in node.SelectNodes("SuperType"))
        Card.Types.Add(new Type(superTypeNode.InnerText));
      Cards.Add(Card);
    }

    //public void ReadXmlCardFile(string FileName)
    //{
    //  XmlDocument doc = new XmlDocument();
    //  doc.Load(FileName);

    //  XmlNodeList simplecreatures = doc.SelectNodes("CardDB/SimpleCreatureCard");
    //  foreach (XmlNode creaturenode in simplecreatures)
    //  {
    //    CreateSimpleCreatureCard(creaturenode);
    //  }
    //}
    //void CreateSimpleCreatureCard(XmlNode node)
    //{
    //  StaticCard Card = new StaticCard();
    //  Card.CardName = node.SelectSingleNode("CardName").InnerText;
    //  Card.Power = System.Convert.ToInt32(node.SelectSingleNode("Power").InnerText);
    //  Card.Toughness = System.Convert.ToInt32(node.SelectSingleNode("Toughness").InnerText);
    //  Card.CastingCosts.SetManaCosts(node.SelectSingleNode("ManaCosts").InnerText);
    //  foreach (XmlNode abilitynode in node.SelectNodes("OneWordAbility"))
    //  {
    //    Card.AddAbility(GetAbility(abilitynode.InnerText));
    //  }

    //  Cards.Add(Card);
    //}

    public StaticCard GetCard(string CardName)
    {
      foreach (StaticCard card in Cards)
      {
        if (card.CardName.ToLower() == CardName.ToLower())
          return card;
      }
      return null;
    }
  }
}
